/*
 * @(#)OpenApiUtil.java   1.0  2021年8月31日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.core.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.swift.core.spring.proxy.AnnotationTypeUtil;
import com.swift.util.bean.AnnotationUtil;
import com.swift.util.bean.BeanUtil;
import com.swift.util.type.TypeUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年8月31日
 */
public class OpenApiUtil {

    private static Map<String,Class<?>> ANNOTATION_CLA_MAP = new HashMap<>();
    
    private static String mapKey(String sysId,String method) {
        return sysId+"+"+method;
    }
    
    public static Class<?> getOpenApi(String sysId,String method){
        if(ANNOTATION_CLA_MAP.containsKey(mapKey(sysId, method))) return ANNOTATION_CLA_MAP.get(mapKey(sysId, method));
        Class<?> claBean = null;
        List<String> list = AnnotationTypeUtil.getAnnotationClassList(OpenApi.class);
        if(TypeUtil.isNull(list)) return null;
        for(String className : list) {
            Class<?> cla = BeanUtil.classForName(className);
            OpenApi openApi = AnnotationUtil.getAnnotation(cla, OpenApi.class);
            if(openApi==null) continue;
            if(!openApi.method().equalsIgnoreCase(method)) continue;
            if(!openApi.value().equalsIgnoreCase(sysId)) continue;
            claBean=cla;
            break;
        }
        ANNOTATION_CLA_MAP.put(mapKey(sysId, method), claBean);
        return claBean;
    }

    public static boolean checkOpenApi(String sysId,String method) {
        Class<?> cla = getOpenApi(sysId, method);
        if(cla==null) return false;
        return true;
    }
}
